package com.boarsoft.flow.service;

import com.boarsoft.flow.core.SimpleFlow;
import com.boarsoft.flow.core.SimpleFlowEngine;

/**
 * 将流程包装为服务
 * 
 * @author Mac_J
 *
 */
public class SimpleFlowServiceImpl implements SimpleFlowService {
	/** */
	protected SimpleFlowEngine simpleFlowEngine;
	/** */
	protected String flowId;

	@Override
	public SimpleFlow create() throws Throwable {
		return simpleFlowEngine.create(flowId);
	}

	@Override
	public Object start(SimpleFlow flow) throws Throwable {
		SimpleFlow f = simpleFlowEngine.start(flow);
		return f.getData();
	}

	@Override
	public Object start(Object data) throws Throwable {
		SimpleFlow f = simpleFlowEngine.start(flowId, data);
		return f.getData();
	}

	@Override
	public Object resume(String id) throws Throwable {
		return this.resume(id, null);
	}

	@Override
	public Object resume(String id, Object exData) throws Throwable {
		SimpleFlow fo = simpleFlowEngine.getFlow(flowId, id);
		if (fo == null) {
			throw new IllegalStateException(//
					String.format("Flow %s/%s not exists", flowId, id));
		}
		fo = simpleFlowEngine.resume(fo, exData);
		return fo.getData();
	}

	public String getFlowId() {
		return flowId;
	}

	public void setFlowId(String flowId) {
		this.flowId = flowId;
	}

	public SimpleFlowEngine getSimpleFlowEngine() {
		return simpleFlowEngine;
	}

	public void setSimpleFlowEngine(SimpleFlowEngine simpleFlowEngine) {
		this.simpleFlowEngine = simpleFlowEngine;
	}
}
